package main

import (

	"whoscored/util/mysql"
	"strings"
	"strconv"
	"fmt"
	"github.com/xuri/excelize"
)

//球队上一个赛季评分、平均进球数、胜率
func seasionRating(team interface{}) []interface{}{

	sql:="select * from team_season where teamId=? and season=? and regionCode=? and tournamentName=?"
	data:=team.(map[string]interface{})
	season:=data["season"].(string)
	seasonTarget:=""
	if data["regionName"]=="Argentina"{
		if season=="2016"{
			seasonTarget="2015"
		}
		if season=="2016/2017"{
			seasonTarget="2016"
		}
		if season=="2017/2018"{
			seasonTarget="2016/2017"
		}
	}else{
		if strings.Contains(season,"/"){
			slice:=strings.Split(season,"/")
			a,_:=strconv.ParseFloat(slice[0],64)
			b,_:=strconv.ParseFloat(slice[1],64)
			seasonTarget=strconv.FormatFloat(a-1,'f',-1,64)+"/"+strconv.FormatFloat(b-1,'f',-1,64)

		}else{
			a,_:=strconv.ParseFloat(season,64)
			seasonTarget=strconv.FormatFloat(a-1,'f',-1,64)
		}
	}


	result:=mysql.FindFirst(sql,data["teamId"],seasonTarget,data["regionCode"],data["tournamentName"])

	if result["rating"]==nil{
		return []interface{}{-9999,-9999}
	}

	return []interface{}{
		result["rating"],result["goals"],
	}
}

//球队上六场比赛的表现
func last6MatchRating(teamHome interface{},teamAway interface{}) ([]interface{},[]interface{}) {
	sql:="select t1.* from team_match t1,team_match t2 " +
		"where t1.matchId=t2.matchId and t1.teamId=? and t1.startTimeUtc<? and t1.regionCode=? and t1.tournamentName=? " +
		"and t2.teamId<>? " +
		"order by t1.startTimeUtc desc limit 6"
	home:=teamHome.(map[string]interface{})
	away:=teamAway.(map[string]interface{})
	result:=mysql.FindAll(sql,home["teamId"],home["startTimeUtc"],home["regionCode"],home["tournamentName"],away["teamId"])

	ff1:=make([]interface{},0)
	for _,v:=range result {
		ff1=append(ff1,v["rating"])
	}
	size:=6-len(ff1)
	for i:=0;i<size ;i++  {
		ff1=append(ff1,-9999)
	}
	//胜平负
	ff2:=make([]interface{},0)
	for _,v:=range result {
		isHome,_:=strconv.ParseFloat(v["isHome"].(string),64)
		homeScore,_:=strconv.ParseFloat(v["homeScore"].(string),64)
		awayScore,_:=strconv.ParseFloat(v["awayScore"].(string),64)
		if (isHome==1&&homeScore-awayScore>0)||(isHome==0&&homeScore-awayScore<0){
			ff2=append(ff2,0)
		}else if(homeScore-awayScore==0){
			ff2=append(ff2,1)
		}else{
			ff2=append(ff2,2)
		}

	}
	size=6-len(ff2)
	for i:=0;i<size ;i++  {
		ff2=append(ff2,-9999)
	}

	return ff1,ff2
}
//上六场对战
func last6FightRating(teamHome interface{},teamAway interface{}) ([]interface{},[]interface{}) {
	sql:="select t1.*,t2.rating as awayRating from team_match t1,team_match t2 " +
		"where t1.matchId=t2.matchId and t1.teamId=? and t1.startTimeUtc<? and t1.regionCode=? and t1.tournamentName=? " +
		"and t2.teamId=? " +
		"order by t1.startTimeUtc desc limit 3"
	home:=teamHome.(map[string]interface{})
	away:=teamAway.(map[string]interface{})
	result:=mysql.FindAll(sql,home["teamId"],home["startTimeUtc"],home["regionCode"],home["tournamentName"],away["teamId"])

	ff1:=make([]interface{},0)
	for _,v:=range result {
		homeRating,_:=strconv.ParseFloat(v["rating"].(string),64)
		awayRating,_:=strconv.ParseFloat(v["awayRating"].(string),64)
		ff1=append(ff1,homeRating-awayRating)
	}
	size:=3-len(ff1)
	for i:=0;i<size ;i++  {
		ff1=append(ff1,-9999)
	}
	//胜平负
	ff2:=make([]interface{},0)
	for _,v:=range result {
		isHome,_:=strconv.ParseFloat(v["isHome"].(string),64)
		homeScore,_:=strconv.ParseFloat(v["homeScore"].(string),64)
		awayScore,_:=strconv.ParseFloat(v["awayScore"].(string),64)
		if (isHome==1&&homeScore-awayScore>0)||(isHome==0&&homeScore-awayScore<0){
			ff2=append(ff2,0)
		}else if(homeScore-awayScore==0){
			ff2=append(ff2,1)
		}else{
			ff2=append(ff2,2)
		}

	}
	size=3-len(ff2)
	for i:=0;i<size ;i++  {
		ff2=append(ff2,-9999)
	}

	return ff1,ff2
}
func scoreWithOdds(fn func([]interface{},int))  {
	sql:="select * from `match` where season<>'2014/2015' and season<>'2015'"
	sql2:="select * from team_match where matchId=? and season<>'2014/2015' and season<>'2015' order by isHome"

	matchs:=mysql.FindAll(sql)
	i:=0
	for _,v:=range matchs {
		data:=mysql.FindAll(sql2,v["matchId"])
		if i==0{

		}

		home:=data[1]
		away:=data[0]
		homeScore,_:=strconv.ParseFloat(v["homeScore"].(string),64)
		awayScore,_:=strconv.ParseFloat(v["awayScore"].(string),64)
		w:=0
		if homeScore-awayScore==0{
			w=1
		}
		if homeScore-awayScore<0{
			w=2
		}
		slice := []interface{}{w}
		h:=seasionRating(home)
		a:=seasionRating(away)
		ff1,ff2:=last6MatchRating(home,away)
		ff3,ff4:=last6MatchRating(away,home)
		ff5,ff6:=last6FightRating(home,away)
		ff:=[][]interface{}{h,a,ff1,ff2,ff3,ff4,ff5,ff6}
		for _, v := range ff {
			for _, vv := range v {
				slice = append(slice, vv)
			}

		}
		i++

		fn(slice, i)
		fmt.Println(i)
	}

}
func main()  {
	e:=[]string{"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"}
	xlsx := excelize.NewFile()

	categories := []string{

	}
	for k,v:=range categories{
		len:=int(k/26)
		s:=""
		if len>0{
			s+=e[len-1]+e[k%26]
		}else{
			s+=e[k%26]
		}

		xlsx.SetCellValue("Sheet1",s+"1", v)
	}
	scoreWithOdds(func(s []interface{}, i int) {
		for k,v:=range s{
			len:=int(k/26)
			s:=""
			if len>0{
				s+=e[len-1]+e[k%26]
			}else{
				s+=e[k%26]
			}
			xlsx.SetCellValue("Sheet1",s+strconv.Itoa(i+1), v)
		}
	})
	err := xlsx.SaveAs("./Workbook.xlsx")
	if err != nil {
		fmt.Println(err)
	}
}